// pages/Systems/AddFeedbackAgain/AddFeedbackAgain.js
//获取应用实例
const app = getApp()
import { updateUserSuggestions } from '../../../api/brand';
import { getGoodsOrder } from '../../../api/order';
import { REFRESH_TOKEN } from '../../../utils/types';
import { checkFile } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadFileUrl: app.globalData.global.uploadFileUrl,
    brandId: app.globalData.global.brandId,
    order: {}, // 资讯订单
    goodsName: '', // 搜索商品名称
    page: 0,
    isReachBottom: false,
    noMore: false,
    loading: false,
    loadingFailed: false,
    dataList: [],
    showOrder: false,
    autosize: { minHeight: 80 },
    fileList: [],
    question: '', // 问题
    detail: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.item){
      var item = JSON.parse(options.item);
      this.setData({
        detail: item,
      })
      if(item.orderCode){
        this._getOrders();
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 查询订单数据
  _getOrders(){
    let _this = this;
    getGoodsOrder({goodsName: _this.data.goodsName,page: _this.data.page,orderCode: _this.data.detail.orderCode})
    .then(async (res) => {
      if(res.success){
        var data = [];
        for (let index = 0; index < res.data.content.length; index++) {
          const item = res.data.content[index];
          item.goodsInfo = JSON.parse(item.goodsInfo);
          item.receivingAddressInfo = JSON.parse(item.receivingAddressInfo);
          data.push(item);
        }
        _this.setData({
          order: data[0],
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 选择图片
  afterRead(event) {
    let _this = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log(file,'file')
    var token = wx.getStorageSync('access_token')
    wx.uploadFile({
      url: _this.data.uploadFileUrl, // 上传接口
      filePath: file.url,
      name: 'file',
      formData: { 'brandId': _this.data.brandId ,'access_token': token,},
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList = [] } = _this.data;
        // fileList.push({ ...file, url: res.data });
        console.log(res,'res')
        var res = JSON.parse(res.data);
        if(res.success){
          var types = checkFile(res.data);
          if(types == 'video'){
            fileList.push({url: res.data,isVideo: true,deletable: true})
          }else if(types == 'image'){
            fileList.push({url: res.data,isImage: true,deletable: true})
          }
          console.log(fileList,'fileList')
          _this.setData({ fileList });
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      },
    });
  },
  // 删除图片
  deleteImg(e){
    console.log(e)
    let list = this.data.fileList
    list.splice(e.detail.index,1)
    this.setData({
      fileList: list,
    })
  },
  qusChange(event) {
    // event.detail 为当前输入的值
    this.setData({
      question: event.detail,
    })
  },
  // 提交反馈
  submit(){
    let _this = this;
    console.log(_this.data.question,'question')
    if(_this.data.question == ''){
      wx.showToast({
        title: '请输入回复问题',
        icon: 'none'
      })
      return;
    }
    var imgUrlArr = [];
    for (let index = 0; index < _this.data.fileList.length; index++) {
      const item = _this.data.fileList[index];
      imgUrlArr.push(item.url);
    }
    var userId = wx.getStorageSync('userId');
    var phone = wx.getStorageSync('phone');
    updateUserSuggestions({id: _this.data.detail.id,content: _this.data.question,contentImgUrl: imgUrlArr.join(','),userId: userId,phone: phone})
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '反馈成功',
          icon: 'success',
        })
        wx.navigateBack({
          delta: 2,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})